#ifdef __TIMER_DELAY
/*
 *delay will be implemented by timer2 in idle mode
 *in the function delay_xx(), the timer2 start, when the time period reached,
 *the timer2 interrupt will end the timer counter
*/
#include "p24FJ16GA002.h"
#include "timer.h"
#include "Terminal.h"
#ifdef __DEBUG
#include <stdio.h>
#endif

extern int old_ipl;
extern T_terminal Terminal;


/*
 *delay 1ms, for the prescale is 1:8, the Resolution of timer2 is 2us
*/
void delay_1ms()
{
    unsigned int period = 500 - 1;
    SET_AND_SAVE_CPU_IPL(old_ipl, 5);
    OpenTimer2(Terminal.runtimePara.HWConfig.Timer2Config.perConfig, period);
#ifdef __DEBUG
//    printf("delay 1ms\n");    
    Nop();
    Nop();
    Nop();
#else
    Idle();
#endif
}
/*

*/
void delay_ms(unsigned int temp_time)
{
    unsigned int period = 500 * temp_time - 1;
    SET_AND_SAVE_CPU_IPL(old_ipl, 5);
    OpenTimer2(Terminal.runtimePara.HWConfig.Timer2Config.perConfig, period);
#ifdef __DEBUG
//    printf("delay %dms\n",temp_time);
    Nop();
    Nop();
    Nop();
#else
    Idle();
#endif
}
/*

*/
void delay_100us()
{
    unsigned int period = 50 - 1;
    SET_AND_SAVE_CPU_IPL(old_ipl, 5);
    OpenTimer2(Terminal.runtimePara.HWConfig.Timer2Config.perConfig, period);
#ifdef __DEBUG
//    printf("delay 100us\n");
    Nop();
    Nop();
    Nop();
#else
    Idle();
#endif
}
/*

*/
void delay_10us()
{
    unsigned int period = 5 - 1;
    SET_AND_SAVE_CPU_IPL(old_ipl, 5);
    OpenTimer2(Terminal.runtimePara.HWConfig.Timer2Config.perConfig, period);
#ifdef __DEBUG
//    printf("delay 10us\n");
    Nop();
    Nop();
    Nop();
#else
    Idle();
#endif
}
#endif //__TIMER_DELAY
